Skip to content

Skip thread-name lookup for outputs that do not use names - #862

Open
jonashaag wants to merge 1 commit into
benfred:masterfrom
jonashaag:skip-unused-thread-name-lookup
Open

Skip thread-name lookup for outputs that do not use names#862
jonashaag wants to merge 1 commit into
benfred:masterfrom
jonashaag:skip-unused-thread-name-lookup

Conversation

@jonashaag

@jonashaag jonashaag commented Jul 17, 2026

Copy link
Copy Markdown

Full disclosure: This has been written by GPT 5.6 Sol.
It is a symptom-treat for #855 (comment) that I consider generally useful.

Summary

  • skip remote thread-name collection when the selected CLI output does not consume names
  • keep collection enabled for dump, speedscope, and every recording using --threads
  • preserve the current default for library users, who can also opt out explicitly through Config

Motivation

Thread names are resolved by walking the target process's sys.modules and the threading module's _active dictionary. This work does not contribute to several outputs:

  • top does not display individual thread names
  • flamegraph and raw recordings use names only when --threads adds a synthetic thread frame
  • chrometrace likewise uses names only with --threads

Avoiding the lookup matters especially when it fails: the name cache remains empty and py-spy retries the remote dictionary walk for every sampled thread on every sample. This change makes the collection decision before PythonSpy::retry_new, so even the validation sample avoids unused lookup work.

Speedscope continues collecting names because it uses them as profile labels. dump and direct PythonSpy users retain the existing behavior by default.

This PR is independent of #861 and applies directly to master.

Validation

  • regression test confirming disabled collection leaves all sampled thread names unset
  • output-policy tests for top, dump, flamegraph, raw, speedscope, and chrometrace with/without --threads
  • pre-commit run --all-files
  • cargo test --release with Python 3.13
  • cargo test --lib
  • cargo check --all-targets
  • cargo check --lib --no-default-features

@jonashaag
jonashaag marked this pull request as ready for review July 17, 2026 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant